package com.dz.risk.controller;

import com.dz.risk.common.ResponseVO;
import com.dz.risk.pojo.domain.ReportFormsEntity;
import com.dz.risk.pojo.model.PageModel;
import com.dz.risk.service.DynamicReportFormsService;
import com.dz.risk.service.ReportFormsDataValueService;
import com.dz.risk.service.ReportFormsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

/**
 * 动态报表数据Api
 *
 * @author Doke
 * @date 2021/7/9 9:20
 */
@Api(tags = "动态报表管理Api")
@RestController
@RequestMapping("/dz-ftsp/stest/easyPoiExportDynamic")
public class DynamicReportFormsServiceController {

    private final DynamicReportFormsService dynamicReportFormsService;

    private final ReportFormsService reportFormsService;

    private final ReportFormsDataValueService reportFormsDataValueService;

    @Autowired
    public DynamicReportFormsServiceController(DynamicReportFormsService dynamicReportFormsService, ReportFormsService reportFormsService, ReportFormsDataValueService reportFormsDataValueService) {
        this.dynamicReportFormsService = dynamicReportFormsService;
        this.reportFormsService = reportFormsService;
        this.reportFormsDataValueService = reportFormsDataValueService;
    }

    /**
     * 动态导入报表模板数据
     */
    @ApiOperation("动态导入报表模板数据")
    @PostMapping("/dynamicImportExcel")
    public ResponseVO dynamicImportExcel(MultipartFile file, ReportFormsEntity reportFormsEntity) throws IOException {
        dynamicReportFormsService.dynamicImportExcel(file, reportFormsEntity);
        return ResponseVO.ok();
    }

    /**
     * 根据报表ID更新动态报表模板数据
     */
    @ApiOperation("更新动态报表模板数据")
    @PostMapping("/updateDynamicExcelById")
    public ResponseVO updateDynamicExcel(MultipartFile file, ReportFormsEntity reportFormsEntity) throws IOException {
        dynamicReportFormsService.updateDynamicExcel(file, reportFormsEntity);
        return ResponseVO.ok();
    }

    /**
     * 根据报表ID删除报表版本数据以及模板数据
     */
    @ApiOperation("根据报表ID删除报表版本数据以及模板数据")
    @ApiImplicitParam(name = "reportFormsId", value = "报表Id", required = true, dataType = "String")
    @GetMapping("/deleteByReportFormsId")
    public ResponseVO deleteByReportFormsId(@RequestParam("reportFormsId") String reportFormsId) {
        dynamicReportFormsService.deleteByReportFormsId(reportFormsId);
        return ResponseVO.ok();
    }

    /**
     * 根据参数查询该报表版本得模板数据内容
     */
    @ApiOperation("根据参数查询该报表版本得模板数据内容")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reportFormsSerialNumber", value = "报表编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "reportFormsBeginDate", value = "报表开始时间 : yyyy-MM-dd", required = true, dataType = "String"),
            @ApiImplicitParam(name = "reportFormsEndDate", value = "报表结束时间 : yyyy-MM-dd", required = true, dataType = "String")
    })
    @GetMapping("/selectDynamicExcelVersionTemplateData")
    public ResponseVO selectDynamicExcelVersionTemplateData(@RequestParam("reportFormsSerialNumber") String reportFormsSerialNumber
            , @RequestParam("reportFormsBeginDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date reportFormsBeginDate
            , @RequestParam("reportFormsEndDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date reportFormsEndDate) {
        return ResponseVO.ok(reportFormsDataValueService.selectReportFormsDataValueByVersionAndSerialNumber(reportFormsSerialNumber, reportFormsBeginDate, reportFormsEndDate));
    }

    @ApiOperation("查询所有报表基本信息(已导入版本得报表信息)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reportFormsName", value = "报表名称", dataType = "String"),
            @ApiImplicitParam(name = "reportFormsSerialNumber", value = "报表编号", dataType = "String"),
            @ApiImplicitParam(name = "beginDate", value = "报表开始时间 : yyyy-MM-dd", dataType = "String"),
            @ApiImplicitParam(name = "endDate", value = "报表结束时间 : yyyy-MM-dd", dataType = "String")
    })
    @GetMapping("/selectVersionReportFormsByParams")
    public ResponseVO selectVersionReportFormsByParams(PageModel pageModel, String reportFormsSerialNumber, String reportFormsName, @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate, @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        return ResponseVO.ok(reportFormsService.selectVersionReportFormsByParams(pageModel, reportFormsSerialNumber, reportFormsName, beginDate, endDate));
    }

}
